package cn.lastcc.recover.contacts.android.sqlite.domain;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import cn.lastcc.recover.contacts.android.entity.domain.ContactsPojo;
import cn.lastcc.recover.contacts.android.sqlite.DBManager;
import cn.lastcc.recover.contacts.android.utils.Constants;

public class DBContacts {

    private DBManager dbManager = null;

    public DBContacts (DBManager dbManager) {
        this.dbManager = dbManager;
    }

    public String findMaxLookUp (String conditions, String [] params) {
        String result = "";
        StringBuilder sql = new StringBuilder("");
        sql.append("select max(lookup) m from contacts where ");
        if ((null == conditions) || ("".equals(conditions.trim()))) {
            conditions = "1 = 1 ";
        }
        sql.append(conditions);
        SQLiteDatabase db = this.dbManager.getDb();
        Cursor cursor = db.rawQuery(sql.toString(), params);
        if (cursor.moveToNext()) {
            result = cursor.getString(cursor.getColumnIndex("m"));
        }
        return result;
    }

    public int insert (ContactsPojo contactsPojo) {
        int count = 0;
        SQLiteDatabase db = this.dbManager.getDb();
        StringBuilder builder = new StringBuilder("");
        builder.append("INSERT INTO contacts ( ");
        builder.append("_id, ");
        builder.append("name_raw_contact_id, ");
        builder.append("photo_id, ");
        builder.append("photo_file_id, ");
        builder.append("custom_ringtone, ");
        builder.append("send_to_voicemail, ");
        builder.append("times_contacted, ");
        builder.append("last_time_contacted, ");
        builder.append("starred, ");
        builder.append("has_phone_number, ");
        builder.append("lookup, ");
//        builder.append("company, ");
//        builder.append("nickname, ");
        builder.append("status_update_id ) ");
        builder.append("VALUES(null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        db.execSQL(builder.toString(), new String [] {
            contactsPojo.getNameRawContactId() + "",
            contactsPojo.getPhotoId() + "",
            contactsPojo.getPhotoFileId() + "",
            contactsPojo.getCustormRingtone(),
            contactsPojo.getSendToVoicemail() + "",
            contactsPojo.getTimesContacted() + "",
            contactsPojo.getLastTimeContacted() + "",
            contactsPojo.getStarred() + "",
            contactsPojo.getHasPhoneNumber() + "",
            contactsPojo.getLookup(),
//            contactsPojo.getCompany(),
//            contactsPojo.getNickname(),
            contactsPojo.getStatusUpdateId() + ""
        });
        Cursor cursor = db.rawQuery("SELECT last_insert_rowid() FROM contacts", null);
        if (cursor.moveToFirst()) {
            count = cursor.getInt(0);
            Log.d(Constants.TAG, "last_insert_rowid is: " + count);
        }
        return count;
    }
}
